﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.IssueNew.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForMaterialtoMO : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //材料出库单
            UFIDA.U9.IssueNew.MaterialDeliveryDocBE.MaterialDeliveryDoc mater = key.GetEntity() as UFIDA.U9.IssueNew.MaterialDeliveryDocBE.MaterialDeliveryDoc;
            if (mater == null) return;

            #region 生产订单是否领料，通过材料出库单审核时回写生产订单是否领料
            //判断为入库单,跳出
            if (mater.IssueType == IssueTypeEnum.Recede) return;
            if (mater.OriginalData.DocState == DocStateEnum.Approving && mater.DocState == DocStateEnum.Approved)
            {
                foreach (var line in mater.MaterialDeliveryDocLines)
                {
                    
                    MO.MO.MO mo = MO.MO.MO.Finder.Find($"DocNo='{line.MODocNo.DocNo}'");
                    if (mo == null) continue;
                    if (mo.DescFlexField.PrivateDescSeg5 == "Yes") continue;
                    using (ISession session = Session.Open())
                    {
                        mo.DescFlexField.PrivateDescSeg5 = "Yes";
                        session.Commit();
                    }
                }
            }



            #endregion

        }

    }
}
